<?php

namespace app\admin\service;

use app\model\WorkersUserModel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\Service;

class WorkersExportService extends Service
{

    /**
     * 批量导出教职工
     * @param array $ids 列表id
     * @return string
     */
    public function WorkersUserExport(array $ids): string
    {
        // 创建新的Spreadsheet对象
        $spreadsheet = new Spreadsheet();
        // 获取活动的工作表
        $sheet = $spreadsheet->getActiveSheet();
        // 设置工作表标题
        $sheet->setTitle('教职工');
        // 设置单元格数据
        $sheet->setCellValue('A1', '教职工姓名');
        $sheet->setCellValue('B1', '出生年月');
        $sheet->setCellValue('C1', '性别');
        $sheet->setCellValue('D1', '身份证号');
        $sheet->setCellValue('E1', '名族');
        $sheet->setCellValue('F1', '籍贯');
        $sheet->setCellValue('G1', '学历');
        $sheet->setCellValue('H1', '职务/职称');
        $sheet->setCellValue('I1', '岗位类型');
        $WorkersUserModel = new WorkersUserModel();
        $data = $WorkersUserModel->where('id', 'in', $ids)->select()->toArray();
        if (!empty($data)){
            foreach ($data as $key => $item) {
                //学历
                $qualification = match ($item['qualification']) {
                    1 => '幼儿园小班',
                    2 => '幼儿园中班',
                    3 => '幼儿园大班',
                    4 => '其它托管教育(不可升级)',
                    5 => '一年级',
                    6 => '二年级',
                    7 => '三年级',
                    8 => '四年级',
                    9 => '五年级',
                    10 => '六年级',
                    11 => '七年级',
                    12 => '八年级',
                    13 => '九年级',
                    14 => '高一年级',
                    15 => '高二年级',
                    16 => '高三年级',
                    17 => '本科一年级',
                    18 => '本科二年级',
                    19 => '本科三年级',
                    20 => '本科四年级',
                    21 => '大专一年级',
                    22 => '大专二年级',
                    23 => '大专三年级',
                    24 => '高职一年级',
                    25 => '高职二年级',
                    26 => '高职三年级',
                    27 => '高职四年级',
                    28 => '高职五年级',
                    29 => '成人继续教育(不可升级)',
                    30 => '职业/技能培训(不可升级)',
                    31 => '研究生(不可升级)',
                    32 => '博士(不可升级)',
                    33 => '其他',
                    default => '--',
                };
                //岗位类型
                $position_type = match ($item['position_type']) {
                    1 => '教师‌',
                    2 => '职员‌',
                    3 => '教学辅助人员',
                    4 => '工勤人员',
                    default => '--',
                };
                // 设置单元格数据
                $key = $key + 2;
                $sheet->setCellValue('A' . $key, $item['name']);
                $sheet->setCellValue('B' . $key, $item['birthday']);
                $sheet->setCellValue('C' . $key, $item['sex'] == 1 ? '男' : '女');
                $sheet->setCellValue('D' . $key, $item['id_card'].' ');
                $sheet->setCellValue('E' . $key, $item['nationality']);
                $sheet->setCellValue('F' . $key, $item['native_place']);
                $sheet->setCellValue('G' . $key, $qualification);
                $sheet->setCellValue('H' . $key, $item['position']);
                $sheet->setCellValue('I' . $key, $position_type);

            }
        }
        $writer = new Xlsx($spreadsheet);
        $ext = '.xlsx';
        $save_name = '/export/workers_user'.date('Ym').'/'.date('d').'_'.time().uniqid();
        $file_path  = dirname('./public' . $save_name.$ext);
        folder_build($file_path);
        $writer->save('./public' . $save_name . $ext);
        return oss('./public' . $save_name . $ext, $ext);
    }


}